<?php

namespace app\jigou\controller\qingdongams\customer;

use app\jigou\controller\qingdongams\Base;
use addons\qingdongams\model\OperationLog;
use think\Db;
use think\Exception;

/**
 * 日志管理
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */
class Log extends Base
{
    /**
     * @var \addons\qingdongams\model\OperationLog
     */
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \addons\qingdongams\model\OperationLog;
    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        $customer_id = input('customer_id','','trim');
        $contacts_id = input('contacts_id','','trim');
        $leads_id = input('leads_id','','trim');
        $contract_id = input('contract_id','','trim');
        $quote_id = input('quote_id','','trim');
        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $wheres = [];
            if(isset($customer_id) && $customer_id){
                $wheres['relation_type'] = $this->model::CUSTOMER_TYPE;
                $wheres['relation_id'] = $customer_id;
            }
            if(isset($contacts_id) && $contacts_id){
                $wheres['relation_type'] = $this->model::CONTACTS_TYPE;
                $wheres['relation_id'] = $contacts_id;
            }
            if(isset($leads_id) && $leads_id){
                $wheres['relation_type'] = $this->model::LEADS_TYPE;
                $wheres['relation_id'] = $leads_id;
            }
            if(isset($contract_id) && $contract_id){
                $wheres['relation_type'] = $this->model::CONTRACT_TYPE;
                $wheres['relation_id'] = $contract_id;
            }
            if(isset($quote_id) && $quote_id){
                $wheres['relation_type'] = $this->model::QUOTE_TYPE;
                $wheres['relation_id'] = $quote_id;
            }
            $list = $this->model->with('staff')->where($where)->where($wheres)
                ->order($sort, $order)->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }

        return $this->view->fetch();
    }
}